﻿using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.VolunteerActivitie
{
    public partial class VolunteerActiviteDetail : PortalPage
    {
        private string ActiviteID
        {
            get
            {
                return this.Request["ID"] ?? "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string activitieId = this.ActiviteID;
            CheckLogin(true);
            PartyVolunteerActivitie activitie = DB.PartyVolunteerActivities.Find(activitieId);
            lblTitle.Text = activitie.Title;
            lblBranch.Text = ControlBusiness.GetBranchPickerText(activitie.BranchID);
            lblTime.Text = Convert.ToDateTime(activitie.BeginTime).ToString("yyyy-MM-dd") + "至" + Convert.ToDateTime(activitie.EndTime).ToString("yyyy-MM-dd");
            lbAddress.Text = activitie.Address;
            ltContent.Text = activitie.Summary;
            ltRecord.Text = activitie.Record;

            IQueryable<PartyVolunteerActivitieMember> q = DB.PartyVolunteerActivitieMembers.Where(t => t.ActivitieID == this.ActiviteID);

            if (q.Count() > 0)
            {
                StringBuilder sbName = new StringBuilder();

                foreach (PartyVolunteerActivitieMember item in q)
                {
                    sbName.Append(ControlBusiness.GetMembersPickerText(item.LoginID) + ",");
                }

                lbJoinMembers.Text = sbName.ToString().TrimEnd(',');
            }

            DataRow[] dataRow = PartyVolunteerActivitieMember.GetGridData(activitieId).Select("Status=2");

            if (dataRow.Length > 0)
            {
                StringBuilder sbName = new StringBuilder();

                foreach (DataRow item in dataRow)
                {
                    sbName.Append(Convert.ToString(item["UserName"]) + ",");
                }

                lblMembers.Text = sbName.ToString().TrimEnd(',');
            }

            //图片
            if (string.IsNullOrEmpty(activitie.ImageID))
            {
                this.trImg.Visible = false;
            }
            else
            {
                this.trImg.Visible = true;
                this.imgH.Src = UpFileBusiness.GetFileUrl(activitie.ImageID);
            }

            //附件
            if (!string.IsNullOrEmpty(activitie.FileID))
            {
                this.aFile.HRef = UpFileBusiness.GetFileUrl(activitie.FileID);
                this.lblFile.Text = UpFileBusiness.GetFileName(activitie.FileID);
                this.trFile.Visible = true;
            }
            else
            {
                this.trFile.Visible = false;
            }

            if (activitie.IsEnd == true)
            {
                btnSubmit.Visible = false;
            }
            else
            {
                q = DB.PartyVolunteerActivitieMembers.Where(t => t.LoginID == this.CurrentUser.LoginID);
                if (q.Count() > 0)
                {
                    btnSubmit.Visible = false;
                }
            }
        }

        protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
        {
            IQueryable<PartyVolunteerActivitieMember> q = DB.PartyVolunteerActivitieMembers.Where(t => t.ActivitieID == this.ActiviteID && t.LoginID == this.CurrentUser.LoginID);

            PartyVolunteerActivitieMember member = new PartyVolunteerActivitieMember();
            member.ID = StringHelper.NewID;
            member.ActivitieID = this.ActiviteID;
            member.LoginID = this.CurrentUser.LoginID;
            member.Status = 1;

            DB.PartyVolunteerActivitieMembers.Add(member);
            DB.SaveChanges();
        }
    }
}